
# ------------------------------------------------------------------------------
# Replication Materials
# 
# title: Coalition Size, Direct Democracy, and Public Spending
# journal: Journal of Public Policy
# authors: Patrick Emmenegger, Lucas Leemann, and André Walter
# date: Sept 2020
# ------------------------------------------------------------------------------




#### Data Manipulation

findat <- data.frame(findat)
findat <- findat %>% distinct(canton, year, .keep_all = TRUE)

refwar <- findat
findat$finex <- ifelse(is.na(findat$refmon_adj),1,0)
findat$numpar <- ifelse(findat$canton == "AI" & findat$year<1930 & findat$year>1865,2,findat$numpar)
findat$numpar <- ifelse(findat$canton == "AR" & findat$year<1895,1,findat$numpar)
findat$left <- ifelse(findat$canton == "AI" & findat$year<1930 & findat$year>1865,0,findat$left)
findat$left <- ifelse(findat$canton == "AR" & findat$year<1895,0,findat$left)
findat <- subset(findat, year %in% seq(1830,2015,5))
findat$LGcant <- 0
findat$LGcant[findat$canton=="AI"|findat$canton=="GL"] <- 1
findat$LGcant[findat$canton=="ZG" & findat$year<1848] <- 1
findat$LGcant[findat$canton=="SZ" & findat$year<1848] <- 1
findat$LGcant[findat$canton=="UR" & findat$year<1928] <- 1
findat$LGcant[findat$canton=="NW" & findat$year<1996] <- 1
findat$LGcant[findat$canton=="AR" & findat$year<1997] <- 1
findat$LGcant[findat$canton=="OW" & findat$year<1998] <- 1

findat$dum1910 <- ifelse(findat$year>1910, 1, 0)

findat$decad <- factor(ifelse(findat$year > 1859 & findat$year <= 1880,1,
                              ifelse(findat$year >1880 & findat$year <=1900,2,
                                     ifelse(findat$year >1900 & findat$year <=1920,3,
                                            ifelse(findat$year >1920 & findat$year <=1940,4,
                                                   ifelse(findat$year >1940 & findat$year <=1960,5,
                                                          ifelse(findat$year >1960 & findat$year <=1980,6,
                                                                 ifelse(findat$year >1980 & findat$year <=2000,7,0))))))))

findat <- findat %>% drop_na(lnexp_pc)
findat <- findat %>% group_by(canton) %>% arrange(canton, year)%>% mutate(lexp_pc = lag(exp_pc), L_numpar = lag(numpar),
                                                                          llnexp_pc = lag(lnexp_pc), dlnexp_pc = lnexp_pc-llnexp_pc,
                                                                          year2 = year, dexp_pc = exp_pc - lexp_pc,
                                                                          polvol = abs(lnexp_pc-llnexp_pc),lpolvol = lag(polvol),
                                                                          fin_ref = pmax(index_Lfin_ref_man,index_ref_mon))

findat$lnfed_us_pc <- log(findat$fed_us_pc + 1)
findat$lnfed_fg_pc <- log(findat$fed_fg_pc + 1)

findat$lnfed_mix[findat$year<1931] <- findat$lnfed_us_pc[findat$year<1931]
findat$lnfed_mix[findat$year>1930] <- findat$lnfed_fg_pc[findat$year>1930]

findat$lnfed_mix[findat$year<1895] <- findat$lnfed_us_pc[findat$year<1895]
findat$lnfed_mix[findat$year>1896] <- findat$lnfed_fg_pc[findat$year>1896]

findat_oLG <- findat[findat$LGcant==0,]


DATA.merge <- as.data.frame(cbind(findat$year,findat$index_law_init,findat$index_law_ref,findat$canton, NA))
colnames(DATA.merge) <- c("year.true", "index_law_init_30", "index_law_ref_30", "canton", "year")
DATA.merge[,1] <- as.numeric(as.character(DATA.merge[,1]))
DATA.merge$year <- DATA.merge[,1]+30
DATA.merge <- DATA.merge[,-1]  
DATA.merge[,1] <- as.numeric(as.character(DATA.merge[,1]))
DATA.merge[,2] <- as.numeric(as.character(DATA.merge[,2]))


findat <- merge(findat,DATA.merge, by=c("year","canton"), all.x=TRUE)

# Canton-specific linear time trends

findat$refmon_adj1 <- ifelse(is.na(findat$refmon_adj), 0, findat$refmon_adj)

findat$refmon_adj2 <- ifelse(is.na(findat$refmon_adj), 70, findat$refmon_adj)
findat$refmon_adj3 <- ifelse(is.na(findat$refmon_adj), 140, findat$refmon_adj)
findat$refmon_nadj3 <- ifelse(is.na(findat$refmon_nadj), 140, findat$refmon_nadj)
findat$refmon_nadj2 <- ifelse(is.na(findat$refmon_nadj), 70, findat$refmon_nadj)
findat$initmin2 <- ifelse(is.na(findat$initmin), 1, findat$initmin)
findat$refmon_adj4 <- findat$refmon_adj3/100
findat$refmon_adj5 <- findat$refmon_adj1/100




findat <- findat %>% group_by(canton) %>% mutate(lrefmon_adj2 = lag(refmon_adj2), difref = refmon_adj2- lrefmon_adj2)
findat$year <- as.numeric(as.character(findat$year))


findat$refmon_adj_imp0 <- findat$refmon_adj4
findat$refmon_adj_imp0[findat$refmon_adj4==1.4] <- 0

findat2 <- subset(findat, year %in% seq(1830,2015,5))


findat3 <- findat2[findat2$year>1899,]
findat_oLG <- findat2[findat2$LGcant==0,]



